#include "pmd.h"
#include <string.h>
#include "iot_pmd.h"
#include "log.h"
static PMD_EVT_CALLBACK s_pmd_evt_callback = 0;
static void* s_pmd_evt_callback_udata = 0;
static void pmd_evt_callback(T_AMOPENAT_PM_MSG* msg){
    if(s_pmd_evt_callback){
        PmdEventParam p;
        p.evtId = msg->evtId;
        switch (msg->evtId)
        {
        case CHR_PRESENT_IND:{
            p.ChargerPresent.present = msg->param.chrpresentind.present;
        }break;
        case BAT_LEVEL_IND:
        case BAT_CHARGING:{
            p.chargingind.batteryLevel = msg->param.chargingind.batteryLevel;
        }break;
        case POWERON_ON_IND:{
            p.poweronind.powerOnReason = msg->param.poweronind.powerOnReason;
        }break;
        default:
            break;
        }
        s_pmd_evt_callback(&p);
    }
}
int platform_pmd_init(int mode,PmdConfig* cfg,PMD_EVT_CALLBACK cb,void* udata){
    T_AMOPENAT_PMD_CFG pmdcfg;
    if(mode == OPENAT_PM_CHR_BY_DEFAULT){
        pmdcfg.deFault.batdetectEnable = TRUE; 
        pmdcfg.deFault.tempdetectEnable = FALSE;
        pmdcfg.deFault.templowLevel = 0;
        pmdcfg.deFault.temphighLevel = 0;
        pmdcfg.deFault.batLevelEnable = FALSE;
    }else{
        //TODO:
    }
    //TODO: mutx
    s_pmd_evt_callback = cb;
    s_pmd_evt_callback_udata = udata;
    if(!iot_pmd_init(mode,&pmdcfg,pmd_evt_callback)){
        return -1;
    }
    return 0;
}
int platform_pmd_poweron_system(int simStartUpMode,int nwStartupMode){
    // if(!iot_pmd_poweron_system(simStartUpMode,nwStartupMode)){
    //     return -1;
    // }
    return 0;
}

int platform_pmd_poweroff_system(){
    iot_pmd_poweroff_system();
    return 0;
}

int platform_pmd_enter_deepsleep(int enter){
    int ret = 0;
    if(enter){
        iot_pmd_enter_deepsleep();
    }else{
        iot_pmd_exit_deepsleep();
    }
    return ret;
}
int platform_get_battery_info(BatteryInfo* pBatteryInfo){
    debugf("platform_get_battery_info");
    BOOL battStatus;
    u16 battVolt;
    u8 battLevel;
    BOOL chargerStatus;
    u8 chargeState;
    // iot_os_sleep(0);
    if(!iot_pmd_get_chg_param(
        &battStatus,
        &battVolt,
        &battLevel,
        &chargerStatus,
        &chargeState
    )){
        TRACE("iot_pmd_get_chg_param FALSE");
        return -1;
    }
    return 0;
}